package com.stx.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.stx.dao.UserDao;
import com.stx.entity.User;

public class UserDaoImpl   implements UserDao{
	
    private HibernateTemplate sessionFactory;
    
	public HibernateTemplate getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(HibernateTemplate sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
    /*
     * 添加用户
     * */
	@Override
	public void addUser(User user) {
		sessionFactory.save(user);
		sessionFactory.flush();
	}
    /*
     * 判断用户是否注册
     * */
	@Override
	public List<User> getUser(User user) {
		List<User>userList=new ArrayList<User>();
		String email=user.getUs_email();
		String hql="from User as user where user.us_email=:e";
		String paramName="e";
		userList=sessionFactory.findByNamedParam(hql,paramName, email);
		return userList;
	}
	/*
	 * 用户的登陆判断
	 * */
	@Override
	public List<User> findUser(User user) {
		List<User>userList=new ArrayList<User>();
		String[] result=new String[]{user.getUs_email(),user.getUs_password()};
		String hql="from User as user where user.us_email=:e and user.us_password=:p";
		String[] paramName=new String[]{"e","p"};
		userList=sessionFactory.findByNamedParam(hql,paramName, result);
		return userList;
	}
	/*
	 * 更新用户操作
	 * */
	@Override
	public void updateUser(User user) {
		String hql="update User as user set user.us_email='"+user.getUs_email()+"',user.us_nickname='"+user.getUs_nickname()+"',user.us_password='"+user.getUs_password()+"',user.us_image='"+user.getUs_image()+"'where user.us_id='"+user.getUs_id()+"'";
		sessionFactory.bulkUpdate(hql);
		sessionFactory.flush();
	}
	/**
	* 使用hql 语句进行操作
	* @param hql HSQL 查询语句
	* @param offset 开始取数据的下标
	* @param length 读取数据记录数
	* @return List 结果集
	*/
	public List getListForPage(final String hql, final int offset,
	final int length) {
	List list =sessionFactory.executeFind(new HibernateCallback() {
	public Object doInHibernate(Session session)
	throws HibernateException, SQLException {
	Query query = session.createQuery(hql);
	query.setFirstResult(offset);
	query.setMaxResults(length);
	List list = query.list();
	return list;
	}
	});
	return list;
	}

	@Override
	public List getAllUser(int firstIndex,int countPage) {
		String hql="from User as user";
		return getListForPage(hql,firstIndex,countPage);
	}
	@Override
	public int getCountUserList() {
		String hql="select count(*) from User as user";
		return ((Long)sessionFactory.find(hql).iterator().next()).intValue();
	}
	@Override
	public User getUserInformation(User user) {
		return sessionFactory.get(User.class, user.getUs_id());
	}
	@Override
	public void delUserInformation(User user) {
		sessionFactory.delete(user);
	}
	@Override
	public void updateAdminUser(User user) {
		String hql="update User as user set user.us_email='"+user.getUs_email()+"',user.us_type='"+user.getUs_type()+"',us_nickname='"+user.getUs_nickname()+"',user.us_password='"+user.getUs_password()+"',user.us_image='"+user.getUs_image()+"'where user.us_id='"+user.getUs_id()+"'";
		sessionFactory.bulkUpdate(hql);
		sessionFactory.flush();
	}

	
}
